package com.xoa.service.equipment.impl;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.JsonArray;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.equipment.EquipmentMapper;
import com.xoa.dao.equipment.LimsEquipInveContMapper;
import com.xoa.dao.equipment.LimsEquipInvePlanMapper;
import com.xoa.dao.equipment.LimsEquipInventoryMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.department.Department;
import com.xoa.model.equipment.LimsEquipInveCont;
import com.xoa.model.equipment.LimsEquipInvePlan;
import com.xoa.model.equipment.LimsEquipInventory;
import com.xoa.model.users.Users;
import com.xoa.service.equipment.EquipmentInventoryService;
import com.xoa.util.LimsJson;
import com.xoa.util.UserUtil;
import com.xoa.util.page.PageParams;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Service
public class EquipmentInventoryServiceImpl implements EquipmentInventoryService {
    @Autowired
    private LimsEquipInventoryMapper limsEquipInventoryMapper;

    @Autowired
    private LimsEquipInveContMapper limsEquipInveContMapper;

    @Autowired
    private LimsEquipInvePlanMapper limsEquipInvePlanMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private DepartmentMapper departmentMapper;
    
    //@Autowired
   // private LimsDeptExpMapper limsDeptExpMapper;


    /**
    * 创建作者:   庞君
    * 创建日期:   15:31 2019/10/21
    * 方法介绍:   添加计划
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson addInvenPlan(String  limsEquipInvePlan , String  limsEquipInveCont, String equips) {
        LimsJson limsJson = new LimsJson();
        try{
            LimsEquipInvePlan limsEquipInvePlan1 = JSONArray.parseObject(limsEquipInvePlan, LimsEquipInvePlan.class);
            LimsEquipInveCont limsEquipInveCont1 = JSONArray.parseObject(limsEquipInveCont, LimsEquipInveCont.class);
            List<LimsEquipInventory> limsEquipInventories= JSONArray.parseArray(equips,LimsEquipInventory.class);
            if(limsEquipInventories!=null){
                String str="";
                int cont=0;
                for(LimsEquipInventory limsEquipInventory :limsEquipInventories){
                    str+=limsEquipInventory.getEquipId()+",";
                    cont++;
                }
                limsEquipInvePlan1.setEquipIds(str);
                limsEquipInvePlan1.setLaveCheck(cont);
                limsEquipInvePlan1.setInveStatus("0");
                int i = limsEquipInvePlanMapper.insertSelective(limsEquipInvePlan1);//优先添加计划表返回主键id
                if(i>0){
                    limsEquipInveCont1.setInveId(limsEquipInvePlan1.getInveId());
                    int re = limsEquipInveContMapper.insertSelective(limsEquipInveCont1);//插入盘点内容表信息
                    if(re>0){
                        if(equips!=null){
                            for(LimsEquipInventory l:limsEquipInventories){
                                l.setInveId(limsEquipInvePlan1.getInveId());
                                l.setInveStatus("0");
                                l.setCheckUser(limsEquipInvePlan1.getCheckUser());
                                int in = limsEquipInventoryMapper.insertSelective(l);//插入设备信息
                            }
                        }
                        limsJson.setCode(0);
                        limsJson.setMsg("新增成功");
                        }else{
                        limsJson.setMsg("新增失败");
                    }

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   16:49 2019/10/21
    * 方法介绍:   查询盘点计划信息
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson InvenPlanList(HttpServletRequest request,Integer page,Integer limit) {
        LimsJson limsJson = new LimsJson();
        Users users = UserUtil.getUser();
        Map map = new HashMap();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(Integer.valueOf(page));
        pageParams.setPageSize(Integer.valueOf(limit));
        map.put("pageParams", pageParams);


        try{
            map.put("userId",users.getUserId());//根据当前登登录人对应盘点人字段查询列表
            List<LimsEquipInvePlan> invenPlanList = limsEquipInvePlanMapper.findInvenPlanList(map);
            if(invenPlanList.size()>0){
                for(LimsEquipInvePlan limsEquipInvePlan:invenPlanList){
                    if(limsEquipInvePlan.getCheckUser()!=null && !limsEquipInvePlan.getCheckUser().equals("")){
                        Users usersByuserId = usersMapper.findUsersByuserId(limsEquipInvePlan.getCheckUser());
                        limsEquipInvePlan.setCheckUser(usersByuserId.getUserName());
                    }
                    LimsEquipInveCont contInfoByInveId = limsEquipInveContMapper.findContInfoByInveId(limsEquipInvePlan.getInveId());
                    limsEquipInvePlan.setLimsEquipInveCont(contInfoByInveId);
                    Department department = departmentMapper.getDeptById(limsEquipInvePlan.getDeptId());
                    // LimsDeptExp deptExpId = limsDeptExpMapper.getDeptExpId(limsEquipInvePlan.getDeptId());
                    limsEquipInvePlan.setDeptName(department.getDeptName());
                    int i = limsEquipInventoryMapper.ContStatus(limsEquipInvePlan.getInveId());
                    if(i>0){
                        limsEquipInvePlan.setLaveCheck(i);
                    }
                }
                limsJson.setCode(0);
                limsJson.setData(invenPlanList);
                limsJson.setCount(pageParams.getTotal());
                limsJson.setMsg("查询成功");
            }else{
                limsJson.setMsg("暂无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   8:38 2019/10/22
    * 方法介绍:   根据盘点Id查询设备明细列表
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson findEquipByInveId(HttpServletRequest request, Integer page, Integer limit, Integer inveId) {
        LimsJson limsJson = new LimsJson();
        Map map = new HashMap();
        PageParams pageParams = new PageParams();//分页
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        pageParams.setUseFlag(true);
        map.put("page",pageParams);
        try{
            map.put("inveId",inveId);
            List<LimsEquipInventory> invenList = limsEquipInventoryMapper.findByInvenId(map);
            if(invenList.size()>0){
                for(LimsEquipInventory limsEquipInventory :invenList){
                    if(limsEquipInventory.getCheckUser()!=null && !limsEquipInventory.getCheckUser().equals("")){
                        Users usersByuserId = usersMapper.findUsersByuserId(limsEquipInventory.getCheckUser());
                        limsEquipInventory.setCheckUser(usersByuserId.getUserName());
                    }
                }
                limsJson.setCode(0);
                limsJson.setData(invenList);
                limsJson.setCount(pageParams.getTotal());
                limsJson.setMsg("查询成功");
            }else{
                limsJson.setCode(0);
                limsJson.setMsg("无数据");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   8:54 2019/10/22
    * 方法介绍:   设备明细详情信息
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson findInvenByDetailId(HttpServletRequest request, Integer detailId) {
        LimsJson limsJson = new LimsJson();
        LimsEquipInventory limsEquipInventory = limsEquipInventoryMapper.selectByPrimaryKey(detailId);
        if(limsEquipInventory!=null){
            limsJson.setCode(0);
            limsJson.setObj(limsEquipInventory);
            limsJson.setMsg("查询成功");
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   13:23 2019/10/22
    * 方法介绍:   删除盘点信息,含批量
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson delInveInfo(HttpServletRequest request, String ids) {
        LimsJson limsJson = new LimsJson();
        String[] idsArray=ids.split(",");
        Map map = new HashMap();
        map.put("ids",idsArray);
        int i = limsEquipInventoryMapper.delInveByInveId(map);
        if(i>0){
            limsEquipInveContMapper.delContByInveId(map);
            int res = limsEquipInvePlanMapper.delInveInfo(map);
            if(res>0){
                limsJson.setMsg("删除成功");
                limsJson.setCode(0);
            }
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   13:44 2019/10/22
    * 方法介绍:   编辑盘点信息回显
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson showEditInfo(HttpServletRequest request, Integer inveId) {
        LimsJson limsJson = new LimsJson();
        List list = new ArrayList();
        LimsEquipInvePlan limsEquipInvePlan = limsEquipInvePlanMapper.selectByPrimaryKey(inveId);
        LimsEquipInveCont contInfoByInveId = limsEquipInveContMapper.findContInfoByInveId(inveId);
        list.add(limsEquipInvePlan);
        list.add(contInfoByInveId);
        limsJson.setCode(0);
        limsJson.setData(list);
        limsJson.setMsg("true");
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   14:24 2019/10/22
    * 方法介绍:   编辑盘点信息
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson editInveInfo(String  limsEquipInvePlan , String  limsEquipInveCont) {
        LimsJson limsJson = new LimsJson();
        LimsEquipInvePlan limsEquipInvePlan1 =JSONArray.parseObject(limsEquipInvePlan,LimsEquipInvePlan.class);
        LimsEquipInveCont limsEquipInveCont1 =JSONArray.parseObject(limsEquipInveCont,LimsEquipInveCont.class);
        if(limsEquipInveCont!=null){
            limsEquipInveContMapper.updateByPrimaryKeySelective(limsEquipInveCont1);
        }
        if(limsEquipInvePlan!=null){
            int i=  limsEquipInvePlanMapper.updateByPrimaryKeySelective(limsEquipInvePlan1);
            if(i>0){
                limsJson.setCode(0);
                limsJson.setMsg("编辑成功");
            }
        }

        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   14:49 2019/10/22
    * 方法介绍:   盘点状态修改
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson upInveStatus(String planStatus, Integer planId,String limsEquipInventory) {
        LimsJson limsJson = new LimsJson();
        Users users = UserUtil.getUser();
        Map map = new HashMap();
        Map hashMap= new HashMap();

        int i=0;
        if(limsEquipInventory!=null){
            /*JSONObject planArray= JSONObject.fromObject(limsEquipInventory);
            LimsEquipInventory limsEquipInve = (LimsEquipInventory) JSONObject.toBean(planArray, LimsEquipInventory.class);*/
            LimsEquipInventory limsEquipInve =JSONArray.parseObject(limsEquipInventory,LimsEquipInventory.class);
            LimsEquipInventory limsEquipInventory1 = limsEquipInventoryMapper.selectByPrimaryKey(limsEquipInve.getDetailId());
            String str1=limsEquipInve.getDeptId()+limsEquipInve.getEquipNo()+limsEquipInve.getEquipName()//将选中字段拼接成字符串进行对比   前端传过来的对象
                    +limsEquipInve.getEquipStatuasCode()+limsEquipInve.getPositionId()+limsEquipInve.getAssetsCost()+limsEquipInve.getManufacturer()+limsEquipInve.getModelNo()+limsEquipInve.getSupplier();
           String str2=limsEquipInventory1.getDeptId()+limsEquipInventory1.getEquipNo()+limsEquipInventory1.getEquipName()  //根据Id获取的对象拼接成字符串
                   +limsEquipInventory1.getEquipStatuasCode()+limsEquipInventory1.getPositionId()+limsEquipInventory1.getAssetsCost()+limsEquipInventory1.getManufacturer()+limsEquipInventory1.getModelNo()+limsEquipInventory1.getSupplier();
            if(str1.equals(str2)){
                limsEquipInve.setWhetherChanges("无");
            }else{
                limsEquipInve.setWhetherChanges("有");
            }
            limsEquipInve.setInveStatus(planStatus);
            limsEquipInve.setCheckTime(new Date());
            limsEquipInve.setCheckUser(users.getUserId());
             i = limsEquipInventoryMapper.updateByPrimaryKeySelective(limsEquipInve);
            if (i>0){
                map.put("inveStatus","0");
                map.put("inveId",limsEquipInve.getInveId());
                hashMap.put("inveId",limsEquipInve.getInveId());
                List<LimsEquipInventory> byInvenId = limsEquipInventoryMapper.findByInvenId(map);
                if(byInvenId.size()>0){//更新计划表中盘点状态
                    hashMap.put("inveStatus","1");
                    limsEquipInvePlanMapper.upbcheckStatus(hashMap);
                }else {
                    hashMap.put("inveStatus","2");
                    limsEquipInvePlanMapper.upbcheckStatus(hashMap);
                }

            }
        }if(planId!=null && planStatus.equals("2")){
            map.put("inveId",planId );
            map.put("inveStatus",planStatus);
             i = limsEquipInvePlanMapper.upbcheckStatus(map);
            map.put("inveStatus","1");
            map.put("whetherChanges","无");
            map.put("checkTime",new Date());
            limsEquipInventoryMapper.upbInveStatus(map);
        }if (i>0){
            limsJson.setCode(0);
            limsJson.setMsg("设备盘点成功");
        }else{
            limsJson.setMsg("盘点失败");
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   9:04 2019/10/23
    * 方法介绍:   盘点数据回显
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson inveShowInfo(Integer inveId,Integer detailId) {
        LimsJson limsJson = new LimsJson();
        LimsEquipInventory limsEquipInventory = new LimsEquipInventory();//新建实体对象接受被选中的设备值
        LimsEquipInveCont contInfoByInveId = limsEquipInveContMapper.findContInfoByInveId(inveId);//根据盘点计划Id查询选中字段
        LimsEquipInventory equipInventory = limsEquipInventoryMapper.selectByPrimaryKey(detailId);//根据设备明细ID进行查询设备信息进行回显
        limsEquipInventory.setEquipId(equipInventory.getEquipId());
        limsEquipInventory.setEquipNo(equipInventory.getEquipNo());
        limsEquipInventory.setEquipName(equipInventory.getEquipName());//获取所有字段值，如果选中状态为1，则将字段值传给新的对象
        if(contInfoByInveId.getDeptIdYn()==1){
            limsEquipInventory.setDeptId(equipInventory.getDeptId());
        }if(contInfoByInveId.getAssetsCostYn()=="1"|| contInfoByInveId.getAssetsCostYn()!=null){
            limsEquipInventory.setAssetsCost(equipInventory.getAssetsCost());
        }if(contInfoByInveId.getEquipNoYn().equals("1")){
            limsEquipInventory.setEquipNo(equipInventory.getEquipNo());
        }if(contInfoByInveId.getEquipStatuasCodeYn()=="1"){
            limsEquipInventory.setEquipStatuasCode(equipInventory.getEquipStatuasCode());
        }if(contInfoByInveId.getFactoryNoYn()=="1"){
            limsEquipInventory.setFactoryNo(equipInventory.getFactoryNo());
        }if(contInfoByInveId.getManufacturerYn()=="1"){
            limsEquipInventory.setManufacturer(equipInventory.getManufacturer());
        }if(contInfoByInveId.getModelNoYn()==1){
            limsEquipInventory.setModelNo(equipInventory.getModelNo());
        }if(contInfoByInveId.getPositionIdYn()!=null&&contInfoByInveId.getPositionIdYn()==1){
            limsEquipInventory.setPositionId(equipInventory.getPositionId());
        }if(contInfoByInveId.getSupplierYn()=="1"){
            limsEquipInventory.setSupplier(equipInventory.getSupplier());
        }
        if(limsEquipInventory!=null){
            limsJson.setCode(0);
            limsJson.setMsg("查询成功");
            limsJson.setObject(limsEquipInventory);
        }else{
            limsJson.setMsg("查询失败");
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   13:12 2019/10/23
    * 方法介绍:同步按钮接口
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson syncEquip(String equipLists) {
        LimsJson limsJson = new LimsJson();
        List<LimsEquipInventory> limsEquipInventoryList = JSONArray.parseArray(equipLists,LimsEquipInventory.class);
        int i=0;
        for(LimsEquipInventory limsEquipInventory :limsEquipInventoryList){
            i += equipmentMapper.updateEquipinventory(limsEquipInventory);
        }
            if(i==limsEquipInventoryList.size()){
                limsJson.setCode(0);
                limsJson.setMsg("同步成功");
            }else{
                limsJson.setMsg("同步失败");
            }


        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   14:03 2019/10/23
    * 方法介绍:   复制盘点计划
    * 参数说明:   No such property: code for class: Script1
    * @return     com.xoa.util.LimsJson
    */
    @Override
    public LimsJson copyInvenPlan(LimsEquipInvePlan limsEquipInvePlan) {
        LimsJson limsJson = new LimsJson();
        Map map = new HashMap();
        Integer inveId=limsEquipInvePlan.getInveId();
        map.put("inveId",inveId);
        LimsEquipInveCont contInfoByInveId = limsEquipInveContMapper.findContInfoByInveId(inveId);
        List<LimsEquipInventory> byInvenId = limsEquipInventoryMapper.findByInvenId(map);
        limsEquipInvePlan.setInveId(null);
        limsEquipInvePlan.setInveStatus("0");
        int i = limsEquipInvePlanMapper.insertSelective(limsEquipInvePlan);
        if(i>0){
            contInfoByInveId.setContId(null);
            contInfoByInveId.setInveId(limsEquipInvePlan.getInveId());
            limsEquipInveContMapper.insertSelective(contInfoByInveId);
            for(LimsEquipInventory limsEquipInventory:byInvenId){
                limsEquipInventory.setDetailId(null);
                limsEquipInventory.setInveStatus("0");
                limsEquipInventory.setInveId(limsEquipInvePlan.getInveId());
                limsEquipInventoryMapper.insertSelective(limsEquipInventory);
            }
            limsJson.setCode(0);
            limsJson.setMsg("复制成功");
        }else{
            limsJson.setMsg("复制失败");
        }
        return limsJson;
    }


}
